package com.cn.wangxin.util;

import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;
import java.util.Map;

/**
 * 功能描述 json转换
 *
 * @Created Time 2016/9/21
 * @Edited Time 2016/9/21
 * @Created by wangxin43
 */
public class JsonUtil {
    private static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * object转json字符串
     * @param o
     * @return
     */
    public static String getObj2Json(Object o){
        try {
            return  objectMapper.writeValueAsString(o);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * json字符串转map
     * @param jsonStr
     * @return
     * @throws IOException
     */
    public static Map<String, Object> parseMap(String jsonStr) throws IOException {
        Map<String, Object> map = objectMapper.readValue(jsonStr, Map.class);
        return map;
    }

    /**
     * json字符串转对象
     * @param var1
     * @param var2
     * @param <T>
     * @return
     */
    public static <T> T json2Obj(String var1, Class<T> var2){
        try {
            return  objectMapper.readValue(var1,var2);
        }catch (IOException e) {
            e.printStackTrace();
        }
        return null;

    }
}
